/*******************************************************************************
 * Copyright (c) 2007 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.sequence.ui.validators;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
import org.eclipse.uml2.uml.MessageSort;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Type;

import com.cea.papyrus.core.PapyrusPlugin;


/**
 * A validator class for transformation selection dialog
 *
 */
public class MessageOperationSelectionStatusValidator implements
		ISelectionStatusValidator {

	private Message message;
	
	public MessageOperationSelectionStatusValidator(Message message) {
		super();
		this.message = message;
	}
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.ISelectionStatusValidator#validate(java.lang.Object[])
	 */
	public IStatus validate(Object[] arg0) {


		if ((arg0 != null) && (arg0.length > 0)) {
			// Single selection
			Object selection = arg0[0];

			if (! (selection instanceof Operation)) {
				return new Status(IStatus.ERROR, PapyrusPlugin.PLUGIN_ID, "No operation selected.");
			}

			Operation currentOp = (Operation) selection;		
			MessageSort sort = message.getMessageSort();
			
			if ((message.getReceiveEvent() != null)
					&& (message.getReceiveEvent() instanceof MessageOccurrenceSpecification)) {
				
				MessageOccurrenceSpecification recvEnd = 
					(MessageOccurrenceSpecification) message.getReceiveEvent();
				Lifeline covered = recvEnd.getCovereds().get(0);
				
				if ((covered.getRepresents() != null) 
						&& (covered.getRepresents().getType() != null)) {

					Type expectedType = covered.getRepresents().getType();
					if (! expectedType.equals(currentOp.getOwner())) {
						return new Status(IStatus.WARNING, PapyrusPlugin.PLUGIN_ID, "Operation from "+expectedType.getName()+" expected.");
					}
				}
			}
			
			
			if (MessageSort.CREATE_MESSAGE_LITERAL.equals(sort)) {
				if (currentOp.getAppliedStereotype("Standard::Create") == null) {
					return new Status(IStatus.WARNING, PapyrusPlugin.PLUGIN_ID, "Create operation expected.");
				}
			}
			
			if (MessageSort.DELETE_MESSAGE_LITERAL.equals(sort)) {
				if (currentOp.getAppliedStereotype("Standard::Destroy") == null) {
					return new Status(IStatus.WARNING, PapyrusPlugin.PLUGIN_ID, "Destroy operation expected.");
				}
			}
			
			if (MessageSort.ASYNCH_CALL_LITERAL.equals(sort)) {
				if (currentOp.getReturnResult() != null) {
					return new Status(IStatus.WARNING, PapyrusPlugin.PLUGIN_ID, "Operation without return value expected.");
				}
			}

			if (MessageSort.SYNCH_CALL_LITERAL.equals(sort)) {
				if (currentOp.getReturnResult() == null) {
					return new Status(IStatus.WARNING, PapyrusPlugin.PLUGIN_ID, "Operation with return value expected.");
				}
			}
			
			if (MessageSort.REPLY_LITERAL.equals(sort)) {
				return Status.OK_STATUS;
			}
			
			return Status.OK_STATUS;

		} else {
			// No selection
			return new Status(IStatus.WARNING, PapyrusPlugin.PLUGIN_ID, "Empty selection.");
		}

		//return new Status(IStatus.ERROR, PapyrusPlugin.PLUGIN_ID, "Unknown error.");
	}

}
